package com.schoolcardinfo.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.schoolcardinfo.beans.ChangeIdInfo;
import com.schoolcardinfo.beans.Id_StudentInfo;
import com.schoolcardinfo.beans.LoseInfo;
import com.schoolcardinfo.beans.RechargeInfo;
import com.schoolcardinfo.beans.SchoolId;
import com.schoolcardinfo.biz.IChangeIdInfoBiz;
import com.schoolcardinfo.biz.IId_StudentInfoBiz;
import com.schoolcardinfo.biz.ILoseInfoBiz;
import com.schoolcardinfo.biz.IRechargeInfoBiz;
import com.schoolcardinfo.biz.ISchoolIdBiz;
import com.schoolcardinfo.biz.impl.ChangeIdInfoBizImpl;
import com.schoolcardinfo.biz.impl.Id_StudentInfoBizImpl;
import com.schoolcardinfo.biz.impl.LoseInfoBizImpl;
import com.schoolcardinfo.biz.impl.RechargeInfoBizImpl;
import com.schoolcardinfo.biz.impl.SchoolIdBizImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class CardInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PrintWriter out;
	private HttpSession session;
	private ISchoolIdBiz schoolIdBiz=new SchoolIdBizImpl();
	private ILoseInfoBiz loseInfoBiz = new LoseInfoBizImpl();
	private IChangeIdInfoBiz changeIdInfoBiz = new ChangeIdInfoBizImpl();
	private IId_StudentInfoBiz studentInfoBiz=new Id_StudentInfoBizImpl();
	private IRechargeInfoBiz rechargeInfoBiz=new RechargeInfoBizImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op=request.getParameter("op");
		session=request.getSession();
		out=response.getWriter();
		
		if("getCardInfo".equals(op)){
			getCardInfo(request,response);
		} else if("addCardInfo".equals(op)){
			addCardInfo(request,response);
		} else if("getStuInfo".equals(op)) {
			getStuInfo(request, response);
		} else if("selectCardInfo".equals(op)){
			selectCardInfo(request,response);
		} else if("selectChangeIdInfo".equals(op)) {
			selectChangeIdInfo(request, response);
		} else if("addChangeIdInfo".equals(op)) {
			addChangeIdInfo(request, response);
		} else if("selectLostInfo".equals(op)) {
			selectLostInfo(request, response);
		} else if("getSidInfo".equals(op)){
			getSidInfo(request, response);
		} else if("rechargeMoney".equals(op)){
			rechargeMoney(request, response);
		} else if("getRechargeInfo".equals(op)){
			getRechargeInfo(request,response);
		} else if("findRecInfo".equals(op)){
			findRecInfo(request,response);
		} else if("getAddCardInfo".equals(op)){
			getAddCardInfo(request,response);
		} else if("getAllLoseInfo".equals(op)){
			getAllLoseInfo(request,response);
		} else if("addLostInfo".equals(op)) {
			addLostInfo(request, response);
		} else if("findChangeIdInfo".equals(op)){
			findChangeIdInfo(request,response);
		} else if("updateLoseInfo".equals(op)){
			updateLoseInfo(request,response);
		}
	}

	//修改挂失卡状态
	private void updateLoseInfo(HttpServletRequest request, HttpServletResponse response) {
		String studentId=request.getParameter("temp");
		int result1=schoolIdBiz.updateLoseStatus(studentId);
		int result2=loseInfoBiz.updateLoseInfo(studentId);
		if(result1+result2 > 1){
			out.print(1);
			out.flush();
			out.close();
		}else{
			out.print(0);
			out.flush();
			out.close();
		}
	}

	//条件查询 换卡信息
	private void findChangeIdInfo(HttpServletRequest request, HttpServletResponse response) {
		String studentId=request.getParameter("studentId");
		List<ChangeIdInfo> list=changeIdInfoBiz.selectChangeIdInfo(Integer.valueOf(studentId));
		if(list==null){
			out.print(0);
			out.flush();
			out.close();
		}else{
			JSONArray json=JSONArray.fromObject(list);
			JSONObject jb=new JSONObject();
			jb.put("rows",json);
			out.print(jb.toString());
			out.flush();
			out.close();
		}
	}

	//查询所有挂失信息
	private void getAllLoseInfo(HttpServletRequest request, HttpServletResponse response) {
		List<LoseInfo> list=loseInfoBiz.selectLostInfo(0, 0);
		if(list!=null){
			JSONArray json=JSONArray.fromObject(list);
			JSONObject jb=new JSONObject();
			jb.put("rows",json);
			out.print(jb.toString());
			out.flush();
			out.close();
		}
	}

	//注册卡时获取学生信息
	private void getAddCardInfo(HttpServletRequest request, HttpServletResponse response) {
		String studentId=request.getParameter("studentId");
		Id_StudentInfo stuInfo=studentInfoBiz.selectId_StudentInfo(Integer.valueOf(studentId));
		if(stuInfo==null){
			out.print(0);
			out.flush();
			out.close();
		}else{
			JSONArray json=JSONArray.fromObject(stuInfo);
			JSONObject jb=new JSONObject();
			jb.put("stuInfo",json);
			out.print(jb.toString());
			out.flush();
			out.close();
		}
	}

	//条件查询充值信息
	private void findRecInfo(HttpServletRequest request, HttpServletResponse response) {
		String myselect=request.getParameter("myselect");
		String info=request.getParameter("info");
		List<RechargeInfo> list=null;
		if(myselect.equals("卡号")){
			list=rechargeInfoBiz.selectRechargeInfo(Integer.valueOf(info));
		}else if(myselect.equals("状态")){
			int status = 0;
			if("成功".equals(info)) {
				status = 1;
			} else {
				status = 0;
			}
			list=rechargeInfoBiz.selectRecStatus(status);
		}
		System.out.println(info+"\t"+list);
		if(list!=null){
			JSONArray json=JSONArray.fromObject(list);
			JSONObject jb=new JSONObject();
			jb.put("rows",json);
			out.print(jb.toString());
			out.flush();
			out.close();
		}else{
			out.print(0);
			out.flush();
			out.close();
		}
	}

	//获取充值信息
	private void getRechargeInfo(HttpServletRequest request, HttpServletResponse response) {
		List<RechargeInfo> list=rechargeInfoBiz.selectRechargeInfo(0);
		if(list!=null){
			JSONArray json=JSONArray.fromObject(list);
			JSONObject jb=new JSONObject();
			jb.put("rows",json);
			out.print(jb.toString());
			out.flush();
			out.close();
		}
	}

	//充值
	private void rechargeMoney(HttpServletRequest request, HttpServletResponse response) {
		int sid=Integer.valueOf(request.getParameter("sid"));
		double rmoney=Double.valueOf(request.getParameter("rmoney"));
		String rtime=request.getParameter("rtime");
		RechargeInfo rechargeInfo=new RechargeInfo(0,rtime,rmoney,sid,1);
		int result1=schoolIdBiz.updateCardMoney(rmoney, sid);
		int result2=rechargeInfoBiz.addRechargeInfo(rechargeInfo);
		if(result1>0 && result2>0){
			out.print(1);
			out.flush();
			out.close();
		}else{
			out.print(0);
			out.flush();
			out.close();
		}
	}

	//查询卡的信息
	private void getSidInfo(HttpServletRequest request, HttpServletResponse response) {
		String sid=request.getParameter("sid");
		List<Map<String,Object>> list=schoolIdBiz.selectSidSchoolCard(Integer.valueOf(sid));
		if(list==null){
			out.print(0);
			out.flush();
			out.close();
		}else if(list.size()>0){
			JSONArray json=JSONArray.fromObject(list);
			JSONObject jb=new JSONObject();
			jb.put("sidInfo",json);
			out.print(jb.toString());
			out.flush();
			out.close();
		}
	}
	
	//添加挂失信息
		private void addLostInfo(HttpServletRequest request, HttpServletResponse response) {
			int studentId = Integer.parseInt(request.getParameter("studentId"));
			String ltime = request.getParameter("ltime");
			String etime = request.getParameter("etime");
			int result1 = schoolIdBiz.updateCardstatus(1, 2, studentId);//修改卡状态
			LoseInfo li = new LoseInfo(0,ltime,studentId,etime,1);
			int result2 = 0;
			if(result1 > 0) {
				result2 = loseInfoBiz.addLostInfo(li);//添加挂失信息
			}
			if(result2 > 0) {
				out.println(1);
			} else {
				out.println(0);
			}
			out.flush();
			out.close();
		}

	//查询挂失信息
	private void selectLostInfo(HttpServletRequest request, HttpServletResponse response) {
		String myselect = request.getParameter("myselect");
		String info = request.getParameter("info");
		List<LoseInfo> list = null;
		if("学号".equals(myselect)) {
			list = loseInfoBiz.selectLostInfo(Integer.parseInt(info), 0);
		} else if("是否领取".equals(myselect)) {
			int status = 0;
			if("已领取".equals(info)) {
				status = 1;
			} else {
				status = 0;
			}
			list = loseInfoBiz.selectLostInfo(0, status);
		}
		if(list!=null){
			JSONArray json=JSONArray.fromObject(list);
			JSONObject jb=new JSONObject();
			jb.put("rows",json);
			out.print(jb.toString());
			out.flush();
			out.close();
		}else {
			out.print(0);
			out.flush();
			out.close();
		}
	}

	//添加换卡信息	1：正常	2：挂失	3：弃用
	private void addChangeIdInfo(HttpServletRequest request, HttpServletResponse response) {
		int result = 0;
		int studentId = Integer.parseInt(request.getParameter("studentId"));
		String cname = request.getParameter("sname");
		String ctime = request.getParameter("ctime");
		SchoolId si = schoolIdBiz.findCardInfo(studentId);
		double balance = si.getBalance();//查出原卡余额
		schoolIdBiz.updateCardMoney(0, si.getSid());//原卡余额清0
		if(schoolIdBiz.updateCardstatus(1 , 3, studentId) > 0) {//更改原卡状态     正常-弃用
			ChangeIdInfo ci = new ChangeIdInfo(0, ctime, studentId, cname);
			if(changeIdInfoBiz.addChangeIdInfo(ci) > 0) {//插入换卡数据
				if(schoolIdBiz.addSchoolId(studentId) > 0) {//添加新卡
					//将余额转入新卡
					result = schoolIdBiz.updateCardMoney(balance, schoolIdBiz.findCardInfo(studentId).getSid());
				} 
			}
		}
		if(result > 0) {
			out.print(result);
		} else {
			out.print(0);
		}
		out.flush();
		out.close();
	}

	//获取换卡信息
	private void selectChangeIdInfo(HttpServletRequest request, HttpServletResponse response) {
		List<ChangeIdInfo> changeInfo = changeIdInfoBiz.selectChangeIdInfo(0);
		if(changeInfo != null){
			JSONArray json = JSONArray.fromObject(changeInfo);
			JSONObject jb = new JSONObject();
			jb.put("rows",json);
			out.print(jb.toString());
			out.flush();
			out.close();
		}
	}

	//卡管理的条件查询
	private void selectCardInfo(HttpServletRequest request, HttpServletResponse response) {
		String myselect=request.getParameter("myselect");
		String info = request.getParameter("info");
		List<Map<String,Object>> list = null;
		if(myselect.equals("学号")){
			list=schoolIdBiz.selectStuIdSchoolCard(Integer.valueOf(info));
		}
		if(myselect.equals("卡号")){
			list=schoolIdBiz.selectSidSchoolCard(Integer.valueOf(info));
		}
		if(myselect.equals("卡状态")){
			int status = 0;
			if("正常".equals(info)) {
				status = 1;
			} else {
				status = 0;
			}
			list=schoolIdBiz.selectStatusSchoolCard(status);
		}
		if(list==null){
			out.print(0);
			out.flush();
			out.close();
		}else if(list.size()>0){
			JSONArray json=JSONArray.fromObject(list);
			JSONObject jb=new JSONObject();
			jb.put("rows",json);
			out.print(jb.toString());
			out.flush();
			out.close();
		}
	}

	//添加学生卡信息
	private void addCardInfo(HttpServletRequest request, HttpServletResponse response) {
		String studentId=request.getParameter("studentId");
		int result1=schoolIdBiz.findStuInfo(Integer.valueOf(studentId));
		if(result1==0){
			int result=schoolIdBiz.addSchoolId(Integer.valueOf(studentId));
			out.print(result);
			out.flush();
			out.close();
		}else{
			out.print(0);
			out.flush();
			out.close();
		}
	}

	//获取校园卡信息
	private void getCardInfo(HttpServletRequest request, HttpServletResponse response) {
		List<Map<String,Object>> list=schoolIdBiz.selectAllSchoolCard();
		if(list!=null){
			JSONArray json=JSONArray.fromObject(list);
			JSONObject jb=new JSONObject();
			jb.put("rows",json);
			out.print(jb.toString());
			out.flush();
			out.close();
		}
	}
	
	//获取学生信息
	private void getStuInfo(HttpServletRequest request, HttpServletResponse response) {
		int studentId = Integer.parseInt(request.getParameter("studentId"));
		Id_StudentInfo studentInfo = studentInfoBiz.selectId_StudentInfo(studentId);
		if(studentInfo != null) {
			JSONArray json=JSONArray.fromObject(studentInfo);
			JSONObject jb=new JSONObject();
			jb.put("stuInfo1",json);
			out.print(jb.toString());
		}else{
			out.print(0);
			
		}
		out.flush();
		out.close();
	}
}
